#include "../../mqserver/binding.hpp"
#include "../../mqcommon/msg.pb.h"
#include <gtest/gtest.h>

std::shared_ptr<BindingManager> managerPtr;
class MsgQueueTest : public testing::Environment
{
public:
    virtual void SetUp() override
    {
        managerPtr = std::make_shared<BindingManager>("ccc/data.db");
    }
    virtual void TearDown() override
    {
        managerPtr->clear();            //最后测试，删表
    }
};
TEST(MsgQueueTest, declareMsgQueueTest)      //recovery也没问题
{
    managerPtr->bind("exchange1", "msgqueue1", "news.music.#");
    managerPtr->bind("exchange1", "msgqueue2", "news.sport.#");
    managerPtr->bind("exchange2", "msgqueue3", "news.music.dj");
    managerPtr->bind("exchange2", "msgqueue2", "news.sport.foot"); //这里例子其实不合理
    EXPECT_EQ(managerPtr->size(), 4);
}
// TEST(MsgQueueTest, deleteMsgQueueTest)
// {
//     managerPtr->unBind("exchange1", "msgqueue2");
//     EXPECT_EQ(managerPtr->size(), 3);
// }

// TEST(MsgQueueTest, removeExchangeBindingTest)     //删除交换机所有相关的binding
// {
//     managerPtr->removeExchangeBinding("exchange1");
//     EXPECT_EQ(managerPtr->size(), 2);
//     EXPECT_EQ(managerPtr->exists("exchange2", "msgqueue1"), true);
// }
// TEST(MsgQueueTest, removeMsgQueueBindingTest)        //删除队列相关的所有绑定信息
// {
//     managerPtr->removeMsgQueueBinding("msgqueue2");
//     EXPECT_EQ(managerPtr->size(), 2);
//     EXPECT_EQ(managerPtr->exists("exchange1", "msgqueue1"), true);
//     EXPECT_EQ(managerPtr->exists("exchange2", "msgqueue1"), true);
// }
TEST(MsgQueueTest, getExchangeBindings)     
{
    MsgQueueBindingMap mp = managerPtr->getExchangeBindings("exchange2");
    EXPECT_EQ(mp.size(), 2);
    EXPECT_NE(mp.find("msgqueue1"), mp.end());
}

TEST(MsgQueueTest, getBinding)     
{
    Binding::Ptr ptr = managerPtr->getBinding("exchange2", "msgqueue1");
    EXPECT_EQ(ptr->exchangeName, "exchange2");
    EXPECT_EQ(ptr->msgQueueName, "msgqueue1");
    EXPECT_EQ(ptr->bindingKey, "news.music.dj");
}
int main(int argc, char* argv[])
{
    testing::InitGoogleTest(&argc, argv);
    testing::AddGlobalTestEnvironment(new MsgQueueTest);

    return RUN_ALL_TESTS(); 
}